
cat('Creating Tables A1 and A2 and Figure 2.1 \n\n')

dat = read.csv('TablesA1A2Figure2_1_data.csv')

non.south.dat = dat[dat$south == 0,]


##regressions for tables
reg1 = lm(raciallychargedsearch~diss+pct.black,
          data = non.south.dat)
        clustered.se = cl(non.south.dat, reg1, non.south.dat$dma_NAME)
        reg1$se = clustered.se

reg2 = lm(raciallychargedsearch~diss+pct.black+I(pct.black^2),
          data = non.south.dat)
        clustered.se = cl(non.south.dat, reg2, non.south.dat$dma_NAME)
        reg2$se = clustered.se

reg3 = lm(raciallychargedsearch~diss*pct.black,
             data = non.south.dat)
          clustered.se = cl(non.south.dat, reg3, non.south.dat$dma_NAME)
          reg3$se = clustered.se

reg4 = lm(raciallychargedsearch~diss*pct.black+diss*I(pct.black^2),
          data = non.south.dat)
        clustered.se = cl(non.south.dat, reg4, non.south.dat$dma_NAME)
        reg4$se = clustered.se


reg5 = lm(raciallychargedsearch~diss+pct.black,
             data = non.south.dat,
             weights = non.south.dat$total.pop)
        clustered.se = cl(non.south.dat, reg5, non.south.dat$dma_NAME)
        reg5$se = clustered.se

reg6 = lm(raciallychargedsearch~diss+pct.black+I(pct.black^2),
             data = non.south.dat,
             weights = non.south.dat$total.pop)
          clustered.se = cl(non.south.dat, reg6, non.south.dat$dma_NAME)
          reg6$se = clustered.se

reg7 = lm(raciallychargedsearch~diss*pct.black,
             data = non.south.dat,
             weights = non.south.dat$total.pop)
          clustered.se = cl(non.south.dat, reg7, non.south.dat$dma_NAME)
          reg7$se = clustered.se

reg8 = lm(raciallychargedsearch~diss*pct.black+diss*I(pct.black^2),
             data = non.south.dat,
             weights = non.south.dat$total.pop)
          clustered.se = cl(non.south.dat, reg8, non.south.dat$dma_NAME)
          reg8$se = clustered.se

reg9 = lm(raciallychargedsearch~diss*pct.black+average.income+pct.college,
          data = non.south.dat,
          weights = non.south.dat$total.pop)
        clustered.se = cl(non.south.dat, reg9, non.south.dat$dma_NAME)
        reg9$se = clustered.se

reg10 = lm(raciallychargedsearch~diss*pct.black+diss*I(pct.black^2)+average.income+pct.college,
          data = non.south.dat,
          weights = non.south.dat$total.pop)
        clustered.se = cl(non.south.dat, reg10, non.south.dat$dma_NAME)
        reg10$se = clustered.se


##print table
coef.names = c('Intercept', 'Segregation', 'Black Population',  'Segregation x Black Population', 'Black Population\verb|^|2','Segregation x Black Population\verb|^|2', 'Average Income', 'Percent College')

outtable = apsrtable(reg1,
                     reg2,
                     reg3,
                     reg4,
                     reg5,
                     reg6,
                     reg7,
                     reg8,
                     reg9,
                     reg10,
                     Sweave = T,
                     coef.names = coef.names,
                     stars = 'default',
                     #model.names = c('UO','Non UO','UO','Non UO','UO','Non UO'),
                     notes = ''
)
writeLines(
  outtable, 'TableA2.tex')


##everybody
##regressions for tables
reg1 = lm(raciallychargedsearch~diss+pct.black,
          data = dat)
      clustered.se = cl(dat, reg1, dat$dma_NAME)
      reg1$se = clustered.se

reg2 = lm(raciallychargedsearch~diss+pct.black+I(pct.black^2),
          data = dat)
        clustered.se = cl(dat, reg2, dat$dma_NAME)
        reg2$se = clustered.se

reg3 = lm(raciallychargedsearch~diss*pct.black,
          data = dat)
        clustered.se = cl(dat, reg3, dat$dma_NAME)
        reg3$se = clustered.se

reg4 = lm(raciallychargedsearch~diss*pct.black+diss*I(pct.black^2),
          data = dat)
        clustered.se = cl(dat, reg4, dat$dma_NAME)
        reg4$se = clustered.se


reg5 = lm(raciallychargedsearch~diss+pct.black,
          data = dat,
          weights = dat$total.pop)
        clustered.se = cl(dat, reg5, dat$dma_NAME)
        reg5$se = clustered.se

reg6 = lm(raciallychargedsearch~diss+pct.black+I(pct.black^2),
          data = dat,
          weights = dat$total.pop)
        clustered.se = cl(dat, reg6, dat$dma_NAME)
        reg6$se = clustered.se

reg7 = lm(raciallychargedsearch~diss*pct.black,
          data = dat,
          weights = dat$total.pop)
        clustered.se = cl(dat, reg7, dat$dma_NAME)
        reg7$se = clustered.se

reg8 = lm(raciallychargedsearch~diss*pct.black+diss*I(pct.black^2),
          data = dat,
          weights = dat$total.pop)
        clustered.se = cl(dat, reg8, dat$dma_NAME)
        reg8$se = clustered.se

reg9 = lm(raciallychargedsearch~diss*pct.black+average.income+pct.college,
          data = dat,
          weights = dat$total.pop)
        clustered.se = cl(dat, reg9, dat$dma_NAME)
        reg9$se = clustered.se

reg10 = lm(raciallychargedsearch~diss*pct.black+diss*I(pct.black^2)+average.income+pct.college,
           data = dat,
           weights = dat$total.pop)
        clustered.se = cl(dat, reg10, dat$dma_NAME)
        reg10$se = clustered.se


##print table
coef.names = c('Intercept', 'Segregation', 'Black Population',  'Segregation x Black Population', 'Black Population\verb|^|2','Segregation x Black Population\verb|^|2', 'Average Income', 'Percent College')

outtable = apsrtable(reg1,
                     reg2,
                     reg3,
                     reg4,
                     reg5,
                     reg6,
                     reg7,
                     reg8,
                     reg9,
                     reg10,
                     Sweave = T,
                     coef.names = coef.names,
                     stars = 'default',
                     #model.names = c('UO','Non UO','UO','Non UO','UO','Non UO'),
                     notes = ''
)
writeLines(
  outtable, 'TableA1.tex')





###############################################
######################

use.places = c('Boston, MA (Manchester, NH)','Phoenix (Prescott), AZ',
               'Los Angeles, CA','Chicago, IL')
use.pop = dat[dat$dma_NAME%in%use.places,]
use.pop$name = as.character(use.pop$dma_NAME)

use.pop$name[use.pop$name == 'Los Angeles, CA'] = 'Los Angeles' 
use.pop$name[use.pop$name == 'Chicago, IL'] = 'Chicago' 
use.pop$name[use.pop$name == 'Boston, MA (Manchester, NH)'] = 'Boston' 
use.pop$name[use.pop$name == 'Phoenix (Prescott), AZ'] = 'Phoenix' 

use.pop$vadjust = use.pop$raciallychargedsearch + c(-1.5,-1,1.5,0)


out.plot.pop = ggplot(dat, aes(pct.black,raciallychargedsearch, label = dma_NAME)) + 
  geom_smooth(color = 'black', aes(alpha = .75)) +
  geom_point(aes(size = total.pop, alpha = 1), color = 'grey67') +
  scale_size_continuous(range = c(.5, 12), breaks=15) + 
  #  geom_text(aes(size = total.pop, alpha = .5)) +
  geom_point(data = use.pop, aes(size = total.pop, alpha = 1), color = 'grey50') +
  geom_text(data = use.pop, aes(pct.black,vadjust, label = name), size = 3.52778, fontface = 'bold') +
  coord_cartesian(ylim = c(20,100), xlim = c(.025,.79)) +
  scale_x_continuous(breaks=c(.2,.4,.6,.8)) +
  theme(legend.position="none") +
  labs(x = 'Black proportion', y = 'Racially charged search') + 
  #  theme_classic()
  #  theme_bw() 
  theme(
    plot.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    axis.text.x=element_text(size=10),
    axis.title.x=element_text(size=12.5),
    axis.title.y=element_text(colour="white"),
    axis.text.y=element_text(colour="white")
  ) 


#########################



out.plot.seg = ggplot(dat, aes(diss,raciallychargedsearch, label = dma_NAME)) + 
  geom_smooth(color = 'black', aes(alpha = .75)) +
  geom_point(aes(size = total.pop, alpha = 1), color = 'grey67') +
  scale_size_continuous(range = c(.5, 12), breaks=15) + 
  #  geom_text(aes(size = total.pop, alpha = .5)) +
  geom_point(data = use.pop, aes(size = total.pop, alpha = 1), color = 'grey50') +
  geom_text(data = use.pop, aes(diss,raciallychargedsearch, label = name), size = 3.52778, fontface = 'bold') +
  scale_x_continuous(breaks=c(.4,.6,.8)) +
  coord_cartesian(ylim = c(20,100), xlim = c(.3,.79)) +
  theme(legend.position="none") +
  labs(x = 'Segregation', y = 'Racially charged search') + 
  theme(
    plot.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    axis.text=element_text(size=10),
    axis.title=element_text(size=12.5)
  ) 




ggsave('Figure2_1Left.jpeg',
       dpi = 1200,
       out.plot.seg,
       height = 4.66,
       width = 3.665,
       units = 'in')

ggsave('Figure2_1Right.jpeg',
       dpi = 1200,
       out.plot.pop,
       height = 4.66,
       width = 3.665,
       units = 'in')

